<?php
include_once 'userConnector.php';
class DBGuestbook extends userConnector {

	protected  $con = null;

	function __construct(){
		//
	}

	public static function getGuestBook($user_id, $offset = 0, $limit = 5){
		$con = self::StartUserConnector();
		$Db_User = new Db_User();
		$offset = $offset*$limit;
		$con->Select("Select * from guestbook where ToUserID = ".$user_id." ORDER BY PostTime DESC LIMIT $offset ,$limit");
		$GuestBooks = $con->fetchobject();
		
		foreach ($GuestBooks as $key => $GuestBook){
    		if($GuestBook->FromNetworkUserID != 0){
    		    $GuestBooks[$key]->FromNetworkUserID = $Db_User->GetUserByID($GuestBook->FromNetworkUserID);
    		}
		}
		return $GuestBooks;
	}
	

    public static function setPost($ToUserID,$PostTitel,$PostText,$FromNetworkUserID = NULL,$FromName = NULL,$FromEmail = NULL){
		$con = self::StartUserConnector();
		
		$sql = "INSERT INTO guestbook (`ID`, `ToUserID`, `PostTime`, `PostTitel`, `PostText`, `FromNetworkUserID`, `FromName`, `FromEmail`, `FromIP`) VALUES (NULL, ".$ToUserID.", CURRENT_TIMESTAMP, '".$PostTitel."', '".$PostText."', '".$FromNetworkUserID."', '".$FromName."', '".$FromEmail."', '".$_SERVER['REMOTE_ADDR']."')";
		$con->Select($sql);
		
		if(!mysql_error()){
		    return true;
		}else{
		    return false;
		}
	}
    public static function getCount($UserID){
		$con = self::StartUserConnector();
		$con->Select('Select count(*) as total from guestbook where ToUserID = '.$UserID);
		$getTotal = $con->fetchobject();
		return $getTotal[0]->total;
	} 
	public static function checkInsertBlock($UserID,$timeinterval){
		$con = self::StartUserConnector();
		$con->Select("SELECT * FROM guestbook WHERE DATE_SUB(NOW(),INTERVAL $timeinterval MINUTE) <= PostTime and ToUserID ='$UserID' and FromIP ='".$_SERVER["REMOTE_ADDR"]."'");

		$getTotal = $con->fetchobject();

		
		if(isset($getTotal[0]->ID)){
		    return false;
		}else{
		    return true;
		}
	}
    public static function getSetting($user_id){
		$con = self::StartUserConnector();
		$con->Select("SELECT * FROM guestbook_setting WHERE user_id ='$user_id'");
		$getTotal = $con->fetchobject();
		
		if(isset($getTotal[0])){
		    return $getTotal[0];
		}else{
		    return false;
		}
	}
    public static function updateDBSetting($user_id,$value){
        $con = self::StartUserConnector();

		$sql = "UPDATE guestbook_setting SET ";
		
		if(array_key_exists("insertTimeBlock",$value)){
		    $sql .= "insertTimeBlock = ".$value['insertTimeBlock'];
		}
        if(array_key_exists("limit",$value)){
		    $sql .= ", `limit` = ".$value['limit'];		    
		}
		
		$sql .= " WHERE user_id = ".$user_id;
		
		$con->Select($sql);
		
        if(!mysql_error()){
		    return true;
		}else{
		    return false;
		}
	}
    public static function setDbSetting($user_id,$insertTimeBlock = 15,$limit =5){
		$con = self::StartUserConnector();
		
		$sql = "INSERT INTO guestbook_setting (`ID`, `user_id`, `insertTimeBlock`, `limit`) VALUES (NULL, ".$user_id.", ".$insertTimeBlock.", ".$limit.")";
		$con->Select($sql);
		
		if(!mysql_error()){
		    return true;
		}else{
		    return false;
		}
	}
    public static function delComment($user_id,$commentId){
		$con = self::StartUserConnector();
		
		$sql = "DELETE FROM `guestbook` WHERE `ID` = $commentId and ToUserID = $user_id LIMIT 1";
		$con->Select($sql);
		
		if(!mysql_error()){
		    return true;
		}else{
		    return false;
		}
	}
	
}

?>